package com.px.framework.mqtt;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.px.framework.mqtt.handler.MqttHandlerSelector;
import com.px.framework.mqtt.model.DeviceTopicEnum;
import com.px.system.domain.CommonTopic;
import com.px.system.domain.vo.PushMqVo;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.px.framework.mqtt.model.TopicConst.*;

/***
 * zhangzheng
 * 消费监听
 * 2023年3月14日 15:39:09
 */
@Component
public class PushCallback implements MqttCallback {

    private static final Logger log = LoggerFactory.getLogger(PushCallback.class);

    @Resource
    private MqttConfiguration mqttConfiguration;

    @Autowired
    private MqttServiceServiceImpl MqttService;

    @Autowired
    private ObjectMapper mapper;

    @Autowired
    MqttHandlerSelector mqttHandlerSelector;

    @Override
    public void connectionLost(Throwable throwable) {
        mqttConfiguration.reconnection();
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        log.info("接收消息主题 : " + topic);
        log.info("接收消息Qos : " + message.getQos());
        log.info("接收消息内容 : " + new String(message.getPayload()));

        //机场消息
        mqttHandlerSelector.handler(topic, message);

        /***
         * 进行业务处理
         * thing/product/#{pid}/*
         * 判断最后一位是什么，然后进行处理
         *
         */
        DeviceTopicEnum topicEnum = DeviceTopicEnum.find(topic);
        String sn = "";
        switch (topicEnum.getBeanName()){

            case THING_MODEL_PRE + STATE_SUF :
                sn = topic.substring((THING_MODEL_PRE + PRODUCT).length(),
                        topic.indexOf(STATE_SUF));

                MqttService.thingStatus(mapper.readValue(message.getPayload(), CommonTopic.class),sn);
                break;
            case THING_MODEL_PRE + SERVICES_SUF + _REPLY_SUF :
                sn = topic.substring((THING_MODEL_PRE + PRODUCT).length(),
                        topic.indexOf(SERVICES_SUF + _REPLY_SUF));

                break;
            case THING_MODEL_PRE + OSD_SUF :
                sn = topic.substring((THING_MODEL_PRE + PRODUCT).length(),
                        topic.indexOf(OSD_SUF));
                MqttService.osdSuf(mapper.readValue(message.getPayload(), CommonTopic.class),sn);
                break;
            /***
             * 1、机场获取上传临时凭证Method: storage_config_get
             *
             */
            case THING_MODEL_PRE + REQUESTS_SUF :
                sn = topic.substring((THING_MODEL_PRE + PRODUCT).length(),
                        topic.indexOf(REQUESTS_SUF));
                MqttService.thingRequests(mapper.readValue(message.getPayload(), CommonTopic.class),sn);
                break;
            /***
             * 1、媒体文件上传结果上报Method: file_upload_callback
             */
            case THING_MODEL_PRE + EVENTS_SUF :
                sn = topic.substring((THING_MODEL_PRE + PRODUCT).length(),
                        topic.indexOf(EVENTS_SUF));
                MqttService.thingEvents(mapper.readValue(message.getPayload(), CommonTopic.class),sn);
                break;
            case THING_MODEL_PRE + PROPERTY_SUF + SET_SUF + _REPLY_SUF :
                sn = topic.substring((THING_MODEL_PRE + PRODUCT).length(),
                        topic.indexOf(PROPERTY_SUF + SET_SUF + _REPLY_SUF));

                break;
            /***
             * 飞行器上传mq
             */
            case WRJ + APP + _SERVICES :
                MqttService.appServices(mapper.readValue(message.getPayload(), PushMqVo.class));

                break;
            /***
             * 行动mq
             */
//            case WRJ + SERVICES_ + _ACTION :
//                MqttService.appPlanServices(mapper.readValue(message.getPayload(), PushMqVo.class));
//
//                break;
            default:
                log.info("未定义主题：" + topic);
                break;
        }
        System.out.println(topicEnum.getBeanName());
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        System.out.println("deliveryComplete---------" + token.isComplete());
    }
}